<template>
  <div id="categories">
    <el-breadcrumb separator="/" style="margin-bottom: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加按钮 -->
      <el-row :gutter="20" style="margin-bottom: 16px">
        <el-col :span="4"
          ><div class="grid-content bg-purple">
            <el-button type="primary" @click="add">添加分类</el-button>
          </div></el-col
        ><el-col :span="10"><div class="grid-content bg-purple"></div></el-col>
      </el-row>
      <!-- 渲染列表 -->
      <el-table
        :data="shoplist"
        style="width: 100%; margin-bottom: 20px"
        row-key="cat_id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column
        ><el-table-column prop="cat_deleted" label="是否有效">
          <i class="el-icon-success"></i>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序">
          <template slot-scope="scope">
            <el-button
              size="mini"
              v-if="scope.row.cat_level == 0"
              type="primary"
              >一级</el-button
            >
            <el-button
              size="mini"
              v-if="scope.row.cat_level == 1"
              type="success"
              >二级</el-button
            >
            <el-button
              size="mini"
              v-if="scope.row.cat_level == 2"
              type="warning"
              >三级</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row.cat_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="40%">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称">
          <el-input v-model="ruleForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <div class="block">
        <span class="demonstration">父级分类</span>
        <el-cascader
          v-model="parent"
          @change="changecascader"
          :options="shoplist"
          :props="{
            checkStrictly: true,
            value: 'cat_id',
            label: 'cat_name',
          }"
          clearable
        ></el-cascader>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  props: [],
  data() {
    return {
      parent: "",
      shoplist: [],
      total: 0, //总数
      pagenum: 1, //当前页
      pagesize: 5, //每页显示条数
      dialogVisible: false, //添加对话框显示
      //添加属性
      ruleForm: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
      },
    };
  },
  methods: {
    //渲染列表
    getshop() {
      this.$http.get("categories").then((res) => {
        this.shoplist = res.data;
        this.total = res.data.length;
      });
    },
    //删除操作
    del(id) {
      this.$http.delete("categories/" + id).then((res) => {
        console.log(res);
        if (res.meta.status == 200) {
          this.getshop();
          this.$message.success("删除成功");
        } else {
          this.$message.info("删除失败");
        }
      });
    },
    //添加分类
    add() {
      this.dialogVisible = true;
    },
    //级联选择器改变触发
    changecascader() {
      if (this.parent == "") {
        this.ruleForm.cat_pid = 0;
      } else if (this.parent.length > 0) {
        this.ruleForm.cat_pid = this.parent[this.parent.length - 1];
      }
      this.cat_level = this.parent.length;
    },
    //点击添加确认按钮
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false;
          this.$http.post("categories", this.ruleForm).then((res) => {
            console.log(res);
            this.getshop();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  computed: {},
  watch: {},
  beforeCreate() {},
  created() {
    this.getshop();
  },
  beforeMounte() {},
  mounted() {},
  beforeUpdate() {},
  updated() {},
  beforeDistroy() {},
  distroyed() {},
};
</script>


<style scoped>
.el-cascader {
  width: 73%;
}
.demonstration {
  margin-left: 30px;
  margin-right: 14px;
}
</style>